package dk.lector;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import dk.lector.domain.TSSystemIntent;
import dk.lector.domain.UserFacade;
import dk.lector.exception.ExceptionHandler;


public class BaseActivity extends Activity {
	
	private String TAG = this.getClass().getSimpleName();
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(this));
		
	}
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Put strings to values
		menu.add(0, 1, 0, "Log out");
		menu.add(0, 2, 0, "TestDialog");
		menu.add(0, 3, 0, "Exit");
		menu.add(0, 4, 0, "About");
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		final Context context = this;
		AlertDialog dialog = new AlertDialog.Builder(this)
		.create();
		switch (item.getItemId()) {
		case 1:
			dialog.setTitle("Log out");
			dialog.setMessage("Are you sure you want to logout?");
			dialog.setButton("I'm sure", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			    	  UserFacade.logOut(context);
			    } });
			dialog.setButton2("Cancel", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			    	  dialog.dismiss();
			    } });
			dialog.show();
			return true;
		case 2:
			dialog.setTitle("Test Menu");
			dialog.setButton("Delete User", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			      
			    } });
			dialog.setButton("Clear Config", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			      
			    } });
			dialog.show();
			return true;
		case 3:
			getBaseContext().sendBroadcast(new TSSystemIntent(R.string.ACTIONCODE_KILL_APPLICATION, TAG));
			return true;
		case 4:
			dialog.setTitle("About");
			dialog.setMessage(getString(R.string.about_content));
			dialog.setButton("Close", new DialogInterface.OnClickListener() {
			      public void onClick(DialogInterface dialog, int which) {
			      dialog.dismiss();
			    } });
			dialog.show();

			return true;
		default:
			return false;

		}
	}
	


}
